package com.dubito.palette

import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.drawable.Drawable
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.Button
import android.widget.ImageView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.BitmapTransitionOptions
import com.bumptech.glide.request.RequestOptions
import com.bumptech.glide.request.RequestOptions.bitmapTransform
import com.bumptech.glide.request.target.CustomTarget
import com.bumptech.glide.request.target.CustomViewTarget
import com.bumptech.glide.request.transition.Transition
import jp.wasabeef.glide.transformations.BlurTransformation

class BlurActivity : AppCompatActivity() {

    private lateinit var imageView: ImageView
    private lateinit var ivBg: ImageView
    private lateinit var btn: Button
    private val images = intArrayOf(R.drawable.fx, R.drawable.fj, R.drawable.jt, R.drawable.pg)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_blur)
        imageView = findViewById(R.id.imageView)
        ivBg = findViewById(R.id.iv_bg)
        btn = findViewById(R.id.change)
        setImage(R.drawable.fj)
        btn.setOnClickListener {
            setImage(images[(0..3).random()])
        }
    }

    private fun setImage(img: Int) {
        Glide.with(this).asBitmap().load(img).apply(bitmapTransform(BlurTransformation(100)))
            .into(object : CustomTarget<Bitmap>() {
                override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
                    ivBg.setImageBitmap(resource)
//                    imageView.setImageBitmap(BitmapFactory.decodeResource(resources ,img))
                    Glide.with(this@BlurActivity).load(img)
                        .apply(RequestOptions.circleCropTransform()).into(imageView)
                }

                override fun onLoadCleared(placeholder: Drawable?) {
                    imageView.setImageDrawable(placeholder)
                }
            })
    }
}